package com.guoxin.jiyin.controller;

import com.alibaba.fastjson.JSON;
import com.guoxin.jiyin.enums.ApiCode;
import com.guoxin.jiyin.model.ApiResult;
import com.guoxin.jiyin.service.ProjectService;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api")
public class ApiController {
    private static final Logger logger = LogManager.getLogger(ApiController.class);

    @Autowired
    private ProjectService projectService;

    @RequestMapping("/createUser")
    @ResponseBody
    public ApiResult createUser(String token, String data) throws Exception {
       projectService.createUser(token, data);
        return ApiResult.resultWith(ApiCode.SUCCESS);
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public ApiResult getUser(String token) throws Exception {
        return ApiResult.resultWith(ApiCode.SUCCESS, projectService.getUser(token));
    }


    /**
     * 上传用户报告
     *
     * @param data
     * @return
     */
    @RequestMapping("/createReport")
    @ResponseBody
    public ApiResult createReport(String token, String data) throws Exception {
        logger.info(data.length());
        projectService.createReport(token, data);
        logger.info("createReport finished");
        return ApiResult.resultWith(ApiCode.SUCCESS);
    }


    /**
     * 获取报告数据
     *
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/getReport")
    @ResponseBody
    public String getReport(String token) throws Exception {
        return projectService.getReport(token);
    }


}
